﻿#region Using Statements
using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using XDL.Framework;
#endregion

namespace XDL.Framework.Screens
{
    /// <summary>
    /// Base class for UI elements displaying some text.
    /// </summary>
    public abstract class TextElement : 
        UIElement
    {
        public static readonly string DefaultFontPath = "2d/Fonts/Normal";

        /// <summary>
        /// Alignment types
        /// </summary>
        public enum TextAlign
        {
            Left,
            Center,
            Right
        }

        TextAlign m_align = TextAlign.Left;
        /// <summary>
        /// Gets/Set alignement for this text-based UI element.
        /// </summary>
        public TextAlign TextAlignment
        {
            get { return m_align; }
            set { m_align = value; }
        }

        #region Fields
        SpriteFont m_font = null;

        /// <summary>
        /// The text rendered for this entry.
        /// </summary>
        string m_text = "";

        #endregion

        #region Properties

        string m_fontName = null;
        /// <summary>
        /// Gets/Sets full path to the font.
        /// </summary>
        public string FontName
        {
            get
            {
                return m_fontName == null ? DefaultFontPath : m_fontName;
            }
            set
            {
                if (string.Compare(value, DefaultFontPath, true) != 0)
                    m_fontName = value;
            }
        }

        public SpriteFont Font
        {
            get
            {
                return m_font;
            }
        }

        /// <summary>
        /// Gets or sets the text of this menu entry.
        /// </summary>
        public string Text
        {
            get { return m_text; }
            set { m_text = value; }
        }
        #endregion

        public TextElement()
        {
        }

        public TextElement(string text)
        {
            m_text = text;
        }

        public override void LoadContent()
        {
            base.LoadContent();

            m_font = GameServices.LoadContent<SpriteFont>(FontName);
        }

        int m_scale = 1;
        public int Scale
        {
            get
            {
                return m_scale;
            }
            set
            {
                m_scale = value;
            }
        }
        /// <summary>
        /// Queries how much space this menu entry requires.
        /// </summary>
        public virtual int GetHeight(MenuScreen screen)
        {
            return m_scale * m_font.LineSpacing;
        }

        public override Vector2 Measure()
        {
            return m_scale * m_font.MeasureString(m_text);
        }

        /// <summary>
        /// Draws the menu entry. This can be overridden to customize the appearance.
        /// </summary>
        protected void Draw(MenuScreen screen, Vector2 position,GameTime gameTime, Color color)
        {
            // Draw text
            ScreenManager screenManager = screen.ScreenManager;
            SpriteBatch spriteBatch = screenManager.SpriteBatch;

            switch (TextAlignment)
            {
                case TextAlign.Left:
                    // do nothing
                    break;
                case TextAlign.Center:
                    position.X += screen.GetDrawArea().Width / 2f - Measure().X / 2f;
                    break;
                case TextAlign.Right:
                    position.X += screen.GetDrawArea().Width - Measure().X;
                    break;
            }

            spriteBatch.DrawString(Font, Text, position, color,0,Vector2.Zero,m_scale,SpriteEffects.None,0);
        }
    }
}
